package sm.upgrade.converter;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sm.dam.api.Converter;

public class CharacterToNumberConverter implements Converter {

	private static Logger logger = LoggerFactory.getLogger(Converter.class);

	@Override
	public Object convert(Object value, Map<String, Object> owner) {
		Object newValue = null;
		if (value instanceof Double) {
			newValue = value;
		} else if (value instanceof String) {
			String stringValue = (String) value;
			try {
				newValue = Double.parseDouble(stringValue);
			} catch (NumberFormatException e) {
				logger.warn("Character value " + stringValue
						+ " can't be parsed to number.");
				// default to 0
				newValue = 0;
			}
		}

		return newValue;
	}

}
